﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Clinica_Frba.Registro_de_LLegada
{
    public partial class RegistroLlegada : Form
    {
        public RegistroLlegada()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label2.Visible = false;
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand com = new SqlCommand("SELECT ID_AFIL FROM DUNDER_MIFFLIN.AFILIADO WHERE ID_AFIL='" + textBox1.Text + "'", myConnection);
            SqlDataReader dr = com.ExecuteReader();
            if (dr.HasRows) { return; } else { label2.Visible = true; }
        }
        private void cargarLista() {
            listBox1.Items.Clear();
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand meds = new SqlCommand("SELECT PROFESIONAL.ID_PROF,PROFESIONAL.Apellido FROM DUNDER_MIFFLIN.PROFESIONAL_ESPECIALIDAD LEFT JOIN DUNDER_MIFFLIN.PROFESIONAL ON DUNDER_MIFFLIN.PROFESIONAL_ESPECIALIDAD.ID_PROF=DUNDER_MIFFLIN.PROFESIONAL.ID_PROF WHERE DUNDER_MIFFLIN.PROFESIONAL_ESPECIALIDAD.ID_ESP=" + comboBox1.SelectedValue, myConnection);
            SqlDataReader dr = meds.ExecuteReader();
            while (dr.Read()){
                
                listBox1.Items.Add(dr.GetInt32(0).ToString()+","+dr.GetString(1));
                
            }
            listBox1.SelectedIndex = 0;
        }
        private void RegistroLlegada_Load(object sender, EventArgs e)
        {
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand esps = new SqlCommand("SELECT * FROM DUNDER_MIFFLIN.ESPECIALIDAD", myConnection);
            SqlDataReader dr = esps.ExecuteReader();
            DataTable dt=new DataTable();
            dt.Columns.Add("ID_ESP");
            dt.Columns.Add("DESC_ESP");
            dt.Load(dr);
            comboBox1.DisplayMember = "DESC_ESP";
            comboBox1.ValueMember = "ID_ESP";
            comboBox1.DataSource = dt;
            comboBox1.SelectedIndex = 0;
            cargarLista();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] todo;
            todo = listBox1.GetItemText(listBox1.SelectedItem).Split(',');
            textBox2.Text = todo[0];
            label4.Visible = false;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            label4.Visible = false;
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand com = new SqlCommand("SELECT ID_PROF FROM DUNDER_MIFFLIN.PROFESIONAL WHERE ID_PROF='" + textBox2.Text + "'", myConnection);
            SqlDataReader dr = com.ExecuteReader();
            if (dr.HasRows) { return; } else { label4.Visible = true; }
       
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string f = publico.fecha;
            if (label2.Visible) { return; } 
            if (label4.Visible) { return; } 
            SqlConnection myConnection = new SqlConnection(publico.ConnectionString);
            myConnection.Open();
            SqlCommand validar = new SqlCommand("SELECT * FROM DUNDER_MIFFLIN.TURNO WHERE ID_AFIL='" + textBox1.Text + "' AND ID_PROF='" + textBox2.Text + "' AND CONVERT(date,fecha,103)=CONVERT(date,'" + f + "',103) AND ESTADO='T'", myConnection);
            
            SqlDataReader dr = validar.ExecuteReader();
            int turno;
            if (dr.HasRows)
            {
                dr.Read();
                turno=dr.GetInt32(0);
                DateTime dtime = dr.GetDateTime(4);
                DateTime ahora = System.DateTime.Now;
                
                TimeSpan diff = dtime - ahora;
                if (diff.Minutes < 0)
                {
                    MessageBox.Show("Llego tarde, turno anulado", "registro de llegada");
                    dr.Close();
                    SqlCommand anul = new SqlCommand("UPDATE DUNDER_MIFFLIN.TURNO SET ESTADO='A' WHERE ID_TURNO=" + turno, myConnection);
                    anul.ExecuteNonQuery();
                }
                else
                {
                    Registro_de_LLegada.Registrar r = new Registrar(dr.GetString(2), dr.GetInt32(1).ToString(), dr.GetInt32(0).ToString());
                    r.Show();
                }
            }
            else { MessageBox.Show("No existen turnos para el afiliado con ese profesional en el dia de la fecha.","Registro de llegada"); }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            cargarLista();
        }
    }
}
